package com.horoscope.astrology.zodiac.signs.free.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.horoscope.astrology.zodiac.signs.free.AppSettingManager
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.NumBerResultBean
import com.horoscope.astrology.zodiac.signs.free.bean.UserBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.EventType
import com.horoscope.astrology.zodiac.signs.free.utils.FlurryEventUtil
import com.horoscope.astrology.zodiac.signs.free.utils.SysUtils
import com.horoscope.astrology.zodiac.signs.free.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_four_number.*

/**
 * 缓存策略：
 * 1、通过日期来判断是否是当日第一次请求 成功->替换日期 失败->清空日期
 * 2、数字的详细解释通过判断从此页面传出的Boolean和是否有数据缓存来判断
 */
class FourNumberActivity : BaseActivity(), View.OnClickListener {

    private var mUserBean: UserBean? = null
    private var isFirstLogin = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_four_number)
        initTitleBar(
            this@FourNumberActivity,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            getString(R.string.text_Numerology)
        )

        mUserBean = AppSettingManager.readObject("user_info")

        daily_number.setOnClickListener(this)
        py_number.setOnClickListener(this)
        e_number.setOnClickListener(this)
        lp_number.setOnClickListener(this)



        if (SysUtils.isTodayFirstLogin(mUserBean!!.constellationName, "LoginDate")) {
            // 只有当天第一次进入才网络拉取
            request()
            isFirstLogin = false
            // 清除相关数字释义缓存
            AppSettingManager.saveString(null, getString(R.string.text_nun1))
            AppSettingManager.saveString(null, getString(R.string.text_nun2))
            AppSettingManager.saveString(null, getString(R.string.text_nun3))
            AppSettingManager.saveString(null, getString(R.string.text_nun4))
        } else {
            isFirstLogin = true
            showLoaded()
            // 缓存里取值
            setNumberText(
                mUserBean!!.number1,
                mUserBean!!.number2,
                mUserBean!!.number3,
                mUserBean!!.number4
            )
        }

//        LiveEventBus.get("FCM", String::class.java).observeSticky(this, Observer<String?> { t ->
//            t.let {
//                Fcm.setText(t)
//            }
//
//        })
    }

    // 顶部四个数字请求
    private fun request() {
        if (mUserBean != null) {
            showLoading()
            NetworkEngine.getNumber(
                mUserBean!!.gender,
                mUserBean!!.birthday,
                mUserBean!!.marial_status, listener
            )
        } else {
            return
        }
    }

    // 顶部四个数字请求回调
    private val listener = object : NetworkListener<NumBerResultBean>() {
        override fun onSuccess(t: NumBerResultBean) {

            showLoaded()
            mUserBean!!.number1 = t.data.content[0].number
            mUserBean!!.number2 = t.data.content[1].number
            mUserBean!!.number3 = t.data.content[2].number
            mUserBean!!.number4 = t.data.content[3].number

            setNumberText(
                t.data.content[0].number,
                t.data.content[1].number,
                t.data.content[2].number,
                t.data.content[3].number
            )

            AppSettingManager.saveObject(mUserBean!!, "user_info")
            AppSettingManager.saveString(SysUtils.getStringDate(), "LoginDate")
            AppSettingManager.saveString(mUserBean!!.constellationName, "CompareConstellation")
        }


        override fun onFailed(t: NumBerResultBean) {
            super.onFailed(t)
            errorMake()
        }

        override fun onError(e: String?) {
            super.onError(e)
            errorMake()
        }

    }

    private fun errorMake() {
        showLoaded()
        AppSettingManager.saveString(null, "LoginDate")
        ToastUtil.showRewardError(this@FourNumberActivity)
    }

    private fun setNumberText(t0: String, t1: String, t2: String, t3: String) {
        dn.text = t0
        pn.text = t1
        en.text = t2
        ln.text = t3
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.daily_number -> {
                FlurryEventUtil.putDataToEvent(EventType.TYPE_NUMEROLOGY_CLICK, "type", "Daily")
                putDataIntent(dn.text.toString(), getString(R.string.text_nun1))
            }
            R.id.py_number -> {
                FlurryEventUtil.putDataToEvent(
                    EventType.TYPE_NUMEROLOGY_CLICK,
                    "type",
                    "Person_Year"
                )
                putDataIntent(pn.text.toString(), getString(R.string.text_nun2))
            }
            R.id.e_number -> {
                FlurryEventUtil.putDataToEvent(
                    EventType.TYPE_NUMEROLOGY_CLICK,
                    "type",
                    "Expression"
                )
                putDataIntent(en.text.toString(), getString(R.string.text_nun3))
            }
            R.id.lp_number -> {
                FlurryEventUtil.putDataToEvent(EventType.TYPE_NUMEROLOGY_CLICK, "type", "Life_Path")
                putDataIntent(ln.text.toString(), getString(R.string.text_nun4))
            }
        }
    }

    private fun putDataIntent(num: String, title: String) {
        val intent = Intent(this, NumberActivity::class.java)
        intent.putExtra("num", num)
        intent.putExtra("title", title)
        if (isFirstLogin) {
            intent.putExtra("isFirstLogin", true)
        } else {
            intent.putExtra("isFirstLogin", false)
        }
        startActivity(intent)
        isFirstLogin = true
    }
}